package com.jshoperxms.controller.mall.backstage.coupon;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.IBaseCtrl;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.mall.backstage.vo.coupon.CouponTCodeVo;
import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.enums.CouponEnums;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.dao.CouponCodeTDao;
import com.jshoperxms.entity.CouponCodeT;
import com.jshoperxms.entity.CouponT;
import com.jshoperxms.service.CouponCodeTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;

/**
 * Created by sdywcd on 17/2/1.
 * Des:优惠券生成编码
 */
@Controller
@RequestMapping("/mall/couponcode")
public class CouponCodeTController extends BaseTController implements IBaseCtrl<CouponTCodeVo> {


    @Resource
    private CouponCodeTService couponCodeTService;

    private Map<String,Object> responsedata=new HashMap<>();

    /**
     * 生成优惠券数量
     * @param rq
     * @return
     * @throws ParseException
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    @Override
    public ResponseEntity save(@RequestBody CouponTCodeVo rq) throws ParseException {
        this.couponCodeTService.saveCouponCodeTransactional(rq);
        responsedata.put(TagWords.SUCCESS, true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        return Response.success(responsedata);
    }

    @Override
    public ResponseEntity find(@RequestBody CouponTCodeVo rq) {
        return null;
    }



    @RequestMapping(value="/del",method= RequestMethod.POST)
    @Override
    public ResponseEntity del(@RequestBody CouponTCodeVo rq) {
        this.couponCodeTService.delCouponCodeTransactional(rq);
        responsedata.put(TagWords.SUCCESS, true);
        responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
        return Response.success(responsedata);
    }

    @Override
    public ResponseEntity update(@RequestBody CouponTCodeVo rq) throws ParseException {
        return null;
    }

    @Override
    public ResponseEntity findAll() {
        return null;
    }

    @RequestMapping(value="/findByPage",method= RequestMethod.POST)
    @Override
    public ResponseEntity findByPage(SearchModel searchModel, @RequestParam("search[value]") String search, @RequestParam("order[0][column]") String ordervalue, @RequestParam("order[0][dir]") String ordertype) {
        findCustomSearch(searchModel,search,ordervalue,ordertype);
        return Response.success(responsedata);
    }

    @Override
    public void findCustomSearch(SearchModel searchModel, String search, String ordervalue, String ordertype) {
        if(searchModel.getIsCustomSearch()==1){
            int currentPage=searchModel.getCalCurrentPage();
            int lineSize = searchModel.getLength();
            Criterion criterion= Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
            if(StringUtils.isNotBlank(searchModel.getCouponrangetype())){
                criterion=Restrictions.and(criterion).add(Restrictions.eq("rangetype",searchModel.getCouponrangetype()));
            }
            if(StringUtils.isNotBlank(searchModel.getCouponid())){
                criterion=Restrictions.and(criterion).add(Restrictions.eq("couponid",searchModel.getCouponid()));
            }
            int records=this.couponCodeTService.count(CouponCodeT.class,criterion).intValue();
            searchModel.setRecordsTotal(records);
            searchModel.setRecordsFiltered(records);
            responsedata.put(TagWords.RECORDSTOTAL,records);
            responsedata.put(TagWords.RECORDSFILTERED,records);
            List<CouponCodeT> list = this.couponCodeTService.findByCriteriaByPage(
                    CouponCodeT.class, criterion, Order.desc(TagWords.UPDATETIME), currentPage,
                    lineSize);
            this.processPageList(list);
        }else{
            if(StringUtils.isNotBlank(search)){
                filterSearch(searchModel,search);
            }else{
                findDefaultAll(searchModel,ordervalue,ordertype);
            }
        }
    }

    @Override
    public void filterSearch(SearchModel searchModel, String search) {
        int currentPage=searchModel.getCalCurrentPage();
        int lineSize = searchModel.getLength();
        Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
        if(StringUtils.isNotBlank(searchModel.getCouponrangetype())){
            criterion=Restrictions.and(criterion).add(Restrictions.eq("rangetype",searchModel.getCouponrangetype()));
        }
        if(StringUtils.isNotBlank(searchModel.getCouponid())){
            criterion=Restrictions.and(criterion).add(Restrictions.eq("couponid",searchModel.getCouponid()));
        }
        int records=this.couponCodeTService.count(CouponCodeT.class,criterion).intValue();
        searchModel.setRecordsTotal(records);
        searchModel.setRecordsFiltered(records);
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        List<CouponCodeT> list = this.couponCodeTService.findByCriteriaByPage(
                CouponCodeT.class, criterion,Order.desc(TagWords.UPDATETIME), currentPage,
                lineSize);
        this.processPageList(list);
    }

    @Override
    public void findDefaultAll(SearchModel searchModel, String ordervalue, String ordertype) {
        int currentPage=searchModel.getCalCurrentPage();
        int lineSize = searchModel.getLength();
        Criterion criterion=Restrictions.ne(TagWords.STATUS, BaseEnums.DataUsingState.DEL.getState());
        if(StringUtils.isNotBlank(searchModel.getCouponrangetype())){
            criterion=Restrictions.and(criterion).add(Restrictions.eq("rangetype",searchModel.getCouponrangetype()));
        }
        if(StringUtils.isNotBlank(searchModel.getCouponid())){
            criterion=Restrictions.and(criterion).add(Restrictions.eq("couponid",searchModel.getCouponid()));
        }
        int records=this.couponCodeTService.count(CouponCodeT.class,criterion).intValue();
        searchModel.setRecordsFiltered(records);
        searchModel.setRecordsTotal(records);
        responsedata.put(TagWords.RECORDSTOTAL,records);
        responsedata.put(TagWords.RECORDSFILTERED,records);
        Order order= BaseTools.orderByParam(TagWords.UPDATETIME,ordervalue,ordertype);
        List<CouponCodeT> list = this.couponCodeTService.findByCriteriaByPage(
                CouponCodeT.class, criterion,order, currentPage,
                lineSize);
        this.processPageList(list);
    }


    @Override
    public void processPageList(List list) {

        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        for(Iterator<CouponCodeT> it=list.iterator();it.hasNext();){
            CouponCodeT bean=it.next();
            Map<String, Object> cellMap = new HashMap<String, Object>();
            cellMap.put("id", bean.getId());
            cellMap.put("rangetype",CouponEnums.RangeType.getName(bean.getRangetype()));
            cellMap.put("bindmemberid", bean.getBindmemberid());
            cellMap.put("code",bean.getCode());
            cellMap.put("status", BaseEnums.DataUsingState.getName(bean.getStatus()));
            cellMap.put("updatetime", DateTools.formateLongDateToString(bean.getUpdatetime().longValue()));
            cellMap.put("version", bean.getVersiont());
            data.add(cellMap);
        }
        responsedata.put(TagWords.DATA, data);
    }

    @Override
    public boolean checkDuplicates(String str) {
        return false;
    }
}
